package com.zbkj.crmeb.chant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.PageParamRequest;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.chant.dao.ChannelProductBatchRecordDao;
import com.zbkj.crmeb.chant.model.ChannelProductBatchRecord;
import com.zbkj.crmeb.chant.response.ChannelProductBatchRecordResponse;
import com.zbkj.crmeb.chant.service.ChannelProductBatchRecordService;
import com.zbkj.crmeb.enums.StockAreaTempEnum;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.service.StoreBrandService;
import com.zbkj.crmeb.store.service.StoreProductService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ChannelProductBatchRecordServiceImpl extends ServiceImpl<ChannelProductBatchRecordDao, ChannelProductBatchRecord> implements ChannelProductBatchRecordService {
    @Autowired
    private StoreProductService storeProductService;
    @Autowired
    private StoreBrandService storeBrandService;

    /**
     * 功能描述: 获取操作记录详情
     * @Param: [InfoId, status, request]
     * @Return: java.util.List<com.zbkj.crmeb.chant.response.ChannelProductBatchRecordResponse>
     * @Author: wangzh
     * @Date: 2023/3/21 17:19
     */
    @Override
    public List<ChannelProductBatchRecordResponse> getListByInfoId(Integer InfoId, Integer status, PageParamRequest request) {
        List<ChannelProductBatchRecordResponse> responses = new ArrayList<>();
        LambdaQueryWrapper<ChannelProductBatchRecord> queryWrapper = new LambdaQueryWrapper<>();
        // 是否全部状态
        if (Objects.nonNull(status)) {
            if (status == 0) {
                queryWrapper.eq(ChannelProductBatchRecord::getBatchStatus, 0);
            } else if (status == 1) {
                queryWrapper.eq(ChannelProductBatchRecord::getBatchStatus, 1);
            }
        }
        queryWrapper.eq(ChannelProductBatchRecord::getInfoId, InfoId);
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<ChannelProductBatchRecord> channelProductBatchRecords = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(channelProductBatchRecords)) {
            return responses;
        }
        // 商品id
        List<Integer> productIds = channelProductBatchRecords.stream().map(ChannelProductBatchRecord::getProductId).distinct().collect(Collectors.toList());
        // 品牌id
        List<Integer> brandIds = channelProductBatchRecords.stream().map(ChannelProductBatchRecord::getBrandId).distinct().collect(Collectors.toList());
        Map<Integer, StoreProduct> storeProductMap = storeProductService.getListInIds(productIds).stream().collect(Collectors.toMap(StoreProduct::getId, e -> e));
        // 品牌id与名称映射关系
        Map<Integer, String> brandNameMap = storeBrandService.getBrandNameMap(brandIds);
        for (ChannelProductBatchRecord channelProductBatchRecord : channelProductBatchRecords) {
            ChannelProductBatchRecordResponse response = new ChannelProductBatchRecordResponse();
            BeanUtils.copyProperties(channelProductBatchRecord, response);
            response.setStoreProduct(storeProductMap.get(channelProductBatchRecord.getProductId()));
            response.setBrandName(brandNameMap.get(channelProductBatchRecord.getBrandId()));
            if (Objects.nonNull(channelProductBatchRecord.getTempId())) {
                response.setTempName(Objects.requireNonNull(StockAreaTempEnum.valueOf(channelProductBatchRecord.getTempId())).getStockAreaName());
            }
            responses.add(response);
        }
        return responses;
    }
}
